package com.yutel.phone.binh.data.net;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.yutel.phone.binh.data.BinhConfig;
import com.yutel.phone.binh.data.net.HttpMapper.DataMap;
import com.yutel.phone.binh.data.vo.Channel;
import com.yutel.phone.binh.data.vo.Video;
import com.yutel.phone.binh.data.vo.VideoDetail;
import com.yutel.phone.binh.data.xmlhandler.SAXforChannelHandler;
import com.yutel.phone.binh.data.xmlhandler.SAXforRecommendHandler;
import com.yutel.phone.binh.data.xmlhandler.SAXforVideoInfoHandler;

public class HttpHelper {
	private final static String TAG = "HttpHelper";

	/**
	 * 数据版本在线查询
	 * 
	 * @return
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	public String queryUpdateInfo() throws IOException,
			ParserConfigurationException, SAXException {
		String url = BinhConfig.serverHost + BinhConfig.app
				+ BinhConfig.updateFile;// 拼接链接地址
		try {
			HttpMapper mapper = new HttpMapper(new DataMap() {
				@Override
				public String data(InputStream input) {
					byte[] data = readStream(input);
					String info = new String(data);
					String aaa[] = info.split("\n");
					return aaa[0];
				}
			});
			return (String) mapper.query(url);
		} catch (Exception e) {
			Log.e(TAG, "queryUpdateInfo()", e);
			return null;
		}
	}

	/**
	 * 视频详细信息查询
	 * 
	 * @param mid
	 * @return
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	public VideoDetail queryVideoInfo(String mid) throws IOException,
			ParserConfigurationException, SAXException {
		String url = BinhConfig.serverHost + BinhConfig.app
				+ BinhConfig.videosPath + mid + ".xml";// 拼接链接地址
		try {
			HttpMapper mapper = new HttpMapper(new DataMap() {
				@Override
				public VideoDetail data(InputStream input) {
					try {
						SAXParserFactory spf = SAXParserFactory.newInstance();
						SAXforVideoInfoHandler videoInfohandle = new SAXforVideoInfoHandler();
						SAXParser saxparser = spf.newSAXParser();
						saxparser.parse(input, videoInfohandle);
						return videoInfohandle.getVedioInfo();
					} catch (Exception e) {
						Log.e(TAG, "queryVideoInfo():", e);
						return null;
					}
				}
			});
			return (VideoDetail) mapper.query(url);
		} catch (Exception e) {
			Log.e(TAG, "queryVideoInfo():", e);
			return null;
		}
	}

	/**
	 * 频道目录查询
	 * 
	 * @param mid
	 * @return
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Channel> queryChannel(String update) throws IOException,
			ParserConfigurationException, SAXException {
		String url = BinhConfig.serverHost + BinhConfig.app + update
				+ BinhConfig.channelFile;// 拼接链接地址
		try {
			HttpMapper mapper = new HttpMapper(new DataMap() {
				@Override
				public ArrayList<Channel> data(InputStream input) {
					try {
						SAXParserFactory spf = SAXParserFactory.newInstance();
						SAXforChannelHandler channelhandler = new SAXforChannelHandler();
						SAXParser saxparser = spf.newSAXParser();
						saxparser.parse(input, channelhandler);
						return channelhandler.getChannels();
					} catch (Exception e) {
						Log.e(TAG, "queryVideoInfo():", e);
						return null;
					}
				}
			});
			return (ArrayList<Channel>) mapper.query(url);
		} catch (Exception e) {
			Log.e(TAG, "queryChannel():", e);
			return null;
		}
	}

	/**
	 * 精彩推荐
	 * 
	 * @param update
	 * @return
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Video> queryRecommend() throws IOException,
			ParserConfigurationException, SAXException {
		String url = BinhConfig.serverHost + BinhConfig.app + BinhConfig.update
				+ BinhConfig.recommendFile;// 拼接链接地址
		try {
			HttpMapper mapper = new HttpMapper(new DataMap() {
				@Override
				public ArrayList<Video> data(InputStream input) {
					try {
						SAXParserFactory spf = SAXParserFactory.newInstance();
						SAXforRecommendHandler recommendhandler = new SAXforRecommendHandler();
						SAXParser saxparser = spf.newSAXParser();
						saxparser.parse(input, recommendhandler);
						return recommendhandler.getVideos();
					} catch (Exception e) {
						Log.e(TAG, "queryVideoInfo():", e);
						return null;
					}
				}
			});
			return (ArrayList<Video>) mapper.query(url);
		} catch (Exception e) {
			Log.e(TAG, "queryChannel():", e);
			return null;
		}
	}

	public ArrayList<Video> pullVediosByCategoryid(String categoryid)
			throws IOException, ParserConfigurationException, SAXException {
		String url = BinhConfig.serverHost + BinhConfig.app + BinhConfig.update
				+ "/" + categoryid + ".xml";// 拼接链接地址
		try {
			HttpMapper mapper = new HttpMapper(new DataMap() {
				@Override
				public ArrayList<Video> data(InputStream input) {
					try {
						SAXParserFactory spf = SAXParserFactory.newInstance();
						SAXforRecommendHandler recommendhandler = new SAXforRecommendHandler();
						SAXParser saxparser = spf.newSAXParser();
						saxparser.parse(input, recommendhandler);
						return recommendhandler.getVideos();
					} catch (Exception e) {
						Log.e(TAG, "queryVideoInfo():", e);
						return null;
					}
				}
			});
			return (ArrayList<Video>) mapper.query(url);
		} catch (Exception e) {
			Log.e(TAG, "queryChannel():", e);
			return null;
		}
	}

	/**
	 * 通过url下载图片
	 * 
	 * @param url
	 * @return
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	public Bitmap getNetBitmap(String url) throws IOException,
			ParserConfigurationException, SAXException {
		try {
			HttpMapper mapper = new HttpMapper(new DataMap() {
				@Override
				public Bitmap data(InputStream input) {
					try {
						Bitmap bitmap = BitmapFactory.decodeStream(input);
						return bitmap;
					} catch (Exception e) {
						Log.e(TAG, "queryVideoInfo():", e);
						return null;
					}
				}
			});
			return (Bitmap) mapper.query(url);
		} catch (Exception e) {
			Log.e(TAG, "queryChannel():", e);
			return null;
		}
	}

	private byte[] readStream(InputStream inputStream) {
		ByteArrayOutputStream outer = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int length = -1;
		try {
			while ((length = inputStream.read(buffer)) != -1) {
				outer.write(buffer, 0, length);
			}
			outer.close();
			inputStream.close();
		} catch (IOException e) {
			return null;
		}
		return outer.toByteArray();
	}
}
